package com.zjw.zy.heart.service.impl;

import com.zjw.zy.heart.domain.AppUiMode;
import com.zjw.zy.heart.mapper.AppUiModeMapper;
import com.zjw.zy.heart.service.IAppUIModeService;
import com.zjw.zy.heart.utils.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AppUIModeService implements IAppUIModeService {

    private static final String TAG = "AppUIModeService";

    private AppUiModeMapper appUiModeMapper;

    private volatile AppUiMode mCurrentUIMode = null;

    @Override
    public AppUiMode requireAppUIMode() {
        if (mCurrentUIMode == null) {
            synchronized (this) {
                mCurrentUIMode = appUiModeMapper.selectByPrimaryKey(0L);
            }
        }
        return mCurrentUIMode;
    }

    @Override
    public boolean updateAppUIMode(int colorStyle) {
        AppUiMode appUiMode = requireAppUIMode();
        appUiMode.setColorStyle(colorStyle);
        int update = appUiModeMapper.updateByPrimaryKey(appUiMode);
        if (update == 0) {
            mCurrentUIMode = appUiMode;
            return true;
        }
        return false;
    }


    @Autowired
    public void setAppUiModeMapper(AppUiModeMapper appUiModeMapper) {
        LogUtils.d(TAG, "success inject AppUiModeMapper");
        this.appUiModeMapper = appUiModeMapper;
    }
}
